package com.file;

import java.io.*;
import java.util.Scanner;

public class 字符流 {
    /**
     * 文件输入字符流
     */
    public void fileReader(){
        FileReader reader = null;
        try {
            reader = new FileReader("d:\\7788\\fuck\\random.txt");
            char[] b = new char[1024];
            int i ;
            while ((i=reader.read(b,0,100)) != -1){
                String s = new String(b,0,100);
                System.out.println(s);
               // System.out.println((char)i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 文件输出字符流
     */
    public void fileWriter(){
        Scanner input = new Scanner(System.in);
        FileWriter fw = null;
        try {
            fw = new FileWriter("D:\\7788\\fuck\\mother.txt"); // 创建FileWriter对象
            for (int i = 0; i < 4; i++) {
                System.out.println("请输入第" + (i + 1) + "个字符串：");
                String name = input.next(); // 读取输入的名称
                fw.write(name + "\r\n"); // 循环写入文件
            }
            System.out.println("录入完成！");
        } catch (Exception e) {
            System.out.println(e.getMessage());
        } finally {
            try {
                fw.close(); // 关闭对象
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 缓冲读取流
     * @param
     */
    public void bufferReader(){
        FileReader reader =  null;
        BufferedReader buffer = null;
        try {
            reader = new FileReader("d:\\7788\\fuck\\sister.txt");
            buffer = new BufferedReader(reader);
            int i ;
            char[] c = new char[1024];
            while ((i=buffer.read(c)) != -1){
                String s = new String(c);
               // System.out.println(s);
                bufferWriter(s);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                buffer.close();
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 缓冲字符输出流
     */
    public void bufferWriter(String s){
        FileWriter file = null;
        BufferedWriter writer = null;
        Scanner scanner = new Scanner(System.in);
        try {
            file = new FileWriter("d:\\7788\\fuck\\girl.txt",true);
            writer = new BufferedWriter(file);

            writer.write(s);

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                writer.close();
                file.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public static void main(String[] args) {
        字符流 stream = new 字符流();
//        stream.fileWriter();
////        stream.fileReader();
       // stream.bufferReader();
        stream.bufferReader();
    }

}
